package com.homework.controller;

import com.homework.dao.TokenMapper;
import com.homework.feign.CodeFeignClient;
import homework.facade.dto.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private TokenMapper tokenMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CodeFeignClient codeFeignClient;

    public static final Map<String ,String> map = new ConcurrentHashMap<>();

    @RequestMapping("/register/{email}/{password}/{code}")
    public Integer register(@PathVariable String email,@PathVariable String password,@PathVariable String code){
        if (StringUtils.isEmpty(code)) {
            return 1;
        }
        Integer checkCodeFlag = codeFeignClient.checkAuthCode(email, code);
//        ResponseEntity<Integer> forEntity =
//                restTemplate.getForEntity("http://localhost:8081/api/code/validate/" + email + "/" +code, Integer.class);
//        Integer checkCodeFlag = forEntity.getBody();
        if (0 == checkCodeFlag) {
            map.put(email,password);
            return 0;
        } else if (1 == checkCodeFlag){
            return 2;
        }
        return 1;

    }

    @RequestMapping("/sRegistered/{email}")
    public Boolean checkRegister(@PathVariable String email){
        String passwork = map.get(email);
        if (StringUtils.isEmpty(passwork)) {
            return true;
        }
        return false;
    }

    @RequestMapping("/login/{email}/{password}")
    public Boolean load(HttpServletResponse httpServletResponse, @PathVariable String email, @PathVariable String password) throws IOException {
        UUID uuid = UUID.randomUUID();
        Token token = new Token();
        token.setToken(uuid.toString());
        token.setEmail(email);
        tokenMapper.addToken(token);

        Cookie cookie = new Cookie("token",uuid.toString());

        httpServletResponse.addCookie(cookie);
        httpServletResponse.sendRedirect("http://www.test.com/static/welcome.html");
        return true;
    }

    @RequestMapping("/info/{token}")
    public String queryEmailByToken(@PathVariable String token){
        Token t = tokenMapper.selectEmail(token);
        if (ObjectUtils.isEmpty(t)) {
            return "no email";
        }
        return t.getEmail();
    }
}
